import 'package:erp/data/api/process_api.dart';
import 'package:erp/data/api/sale_project_api.dart';
import 'package:erp/data/model/process_file.dart';
import 'package:erp/data/model/process_handle.dart';
import 'package:erp/data/model/sale_project_detail.dart';
import 'package:erp/routes/app_pages.dart';
import 'package:erp/util/app_utils.dart';
import 'package:erp/widgets/form1/form_builder.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class SaleProjectHandleController extends GetxController {
  final List<FilterChipItem> docListItems = [
    FilterChipItem(label: Text('设计概算'), data: '1'),
    FilterChipItem(label: Text('施工图纸'), data: '2'),
    FilterChipItem(label: Text('总包投标报价清单'), data: '3'),
    FilterChipItem(label: Text('招标文件'), data: '4'),
    FilterChipItem(label: Text('施工量清单'), data: '5'),
    FilterChipItem(label: Text('技术要求'), data: '6'),
  ];
  final List<RadioItem<String>> radioItems = [
    RadioItem(label: '同意', data: 'YES'),
    RadioItem(label: '退回', data: 'NO'),
  ];

  // 流程信息
  Map<String, dynamic> processData = {};

  // 市场立项项目信息
  Map<String, dynamic> saleProjectData = {};

  // 项目合同信息
  List<SaleProjectDetail> saleProjectDetails = [];

  // 流程附件信息
  List<ProcessFile> processAttachmentList = [];

  // 流程节点信息
  List<ProcessHandle> processHandleList = [];

  // 是否同意
  var handleResult = "".obs;

  // 节点 可读
  var nodeAuth = 0.obs;

  late final ProcessHandle handle;

  FormManagement formManagement = FormManagement();

  Map<String, dynamic> initialValues = {'handleResult': "YES"};

  @override
  void onInit() async {
    var data = Get.arguments['data'];
    processData = data['process'];
    processAttachmentList = (data['processFileList'] as List).map((e) => ProcessFile.fromJson(e)).toList();
    var object = data['object'];
    saleProjectDetails = (object['saleProjectDetailList'] as List).map((e) => SaleProjectDetail.fromJson(e)).toList();
    saleProjectData = object['saleProject'];
    saleProjectData['docList'] = (object['saleProjectDocList'] as List).map((e) => e['docCode'].toString()).toList();
    this.handle =
        ProcessHandle.fromJson(await ProcessApi.selectNearHandleProcessHandleByProcessNo(processData['processNo']));
    nodeAuth.value = handle.nodeAuth;

    super.onInit();
  }

  void seeProjectDetail(item) {
    // 先保存下数据
    var formData = formManagement.data;
    initialValues.addAll(formData);
    Get.toNamed(Routes.SALE_PROJECT_DETAILS, arguments: {'data': item.toJson(), 'readOnly': true});
  }

  void cancel() {
    Get.back();
  }

  /// 下一步
  void next() async {
    var validateResult1 = formManagement.validate();
    if (!validateResult1.result) {
      EasyLoading.showError(validateResult1.message ?? "");
      return;
    }
    var formData = formManagement.data;

    var _handle = handle.toJson();
    _handle["handleResult"] = formData["handleResult"];
    _handle["handleContent"] = formData["handleContent"];
    AppUtils.confirm(
      content: "是否确认转交下一步？",
      confirmCallback: () async {
        if (nodeAuth == 1) {
          saleProjectData["saleProjectData"] = formData["saleProjectData"];
          saleProjectData["projectExpenseAmount"] = formData["projectExpenseAmount"];
          await SaleProjectApi.saveSaleProjectExtra(saleProjectData);
          await ProcessApi.saveProcessHandle(_handle);
        } else {
          await ProcessApi.saveProcessHandle(_handle);
        }
        Get.back(result: true);
      },
    );
  }
}
